let app = getApp();
let audioInfo = app.globalData.audioInfo;

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    kitapCon: {
      type: Object,
      value: ''
    },
    play: {
      type: Boolean,
      value: true
    },
    chapterName: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    playAllMask: false, // 购买会员提示
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击播放最小画
    onClickLittleDrawing() {
      let postId = app.globalData.kitapCon.id;
      
      wx.navigateTo({
        url: `/pages/audioPlay/audioPlay?postId=` + postId,
      })
    },

    // 最小画 播放、暂停
    onPlay() {

      // 如果正在播放音频时停止音频
      if (audioInfo.isPlaying) {
        audioInfo.isPlaying = false;
        this.setData({ play: false });
        
        return audioInfo.bgAudio.pause();
      } else if (!audioInfo.isPlaying) {
        audioInfo.isPlaying = true;
        this.setData({ play: true,  });
        
        return audioInfo.bgAudio.play();
      }

    },

    // 最小画 下一曲
    previousOne () {

      // 检查是否免费或者是否购买会员
      let isFree = app.globalData.kitapCon.is_free;
      if(isFree==1 && !app.globalData.isVIPAuthority) return this.setData({playAllMask: true})

      const { audioList } = app.globalData;
      if (audioList.length === 0) return;

      const currentIndex = audioList.findIndex(item => item.id === audioInfo.playId);
      
      // 计算上一首的索引（支持循环播放）
      const prevIndex = (currentIndex - 1 + audioList.length) % audioList.length;
      const prevSong = audioList[prevIndex];
      audioInfo.bgAudio.stop();

      // 创建后台播放
      audioInfo.bgAudio = wx.getBackgroundAudioManager()
      audioInfo.bgAudio.title = prevSong.name;
      audioInfo.bgAudio.singer = prevSong.chapter_name;
      audioInfo.bgAudio.coverImgUrl = this.data.kitapCon.cover_wx_url;
      audioInfo.bgAudio.src = prevSong.wx_url;
      audioInfo.playId = prevSong.id;

      // 更改章节名
      app.globalData.chapterName = prevSong.chapter_name;
      this.setData({
        chapterName: prevSong.chapter_name
      })

    },

    // 最小画 上一曲
    nextOne () {

      // 检查是否免费或者是否购买会员
      let isFree = app.globalData.kitapCon.is_free;
      if(isFree==1 && !app.globalData.isVIPAuthority) return this.setData({playAllMask: true})

      const { audioList } = app.globalData;
      if (audioList.length === 0) return; // 没有歌曲时直接返回

      // 找到当前歌曲的索引
      const currentIndex = audioList.findIndex(item => item.id === audioInfo.playId);
      
      // 计算下一首的索引（支持循环播放）
      const nextIndex = (currentIndex + 1) % audioList.length;
      const nextSong = audioList[nextIndex];
      audioInfo.bgAudio.stop();

      // 创建后台播放
      audioInfo.bgAudio = wx.getBackgroundAudioManager()
      audioInfo.bgAudio.title = nextSong.name;
      audioInfo.bgAudio.singer = nextSong.chapter_name;
      audioInfo.bgAudio.coverImgUrl = this.data.kitapCon.cover_wx_url;
      audioInfo.bgAudio.src = nextSong.wx_url;
      audioInfo.playId = nextSong.id;

      // 更改章节名
      app.globalData.chapterName = nextSong.chapter_name;
      this.setData({
        chapterName: nextSong.chapter_name
      })

    },
  },
  lifetimes: {
    created() {
      
    }
  },
})